<?php
namespace ZyucMini\Core;

class Cache
{
    /**
     * 缓存目录
     * @var string
     */
    private $cacheDir;

    /**
     * 构造函数
     * @param string $cacheDir 缓存目录名称
     */
    public function __construct($cacheDir = 'cache')
    {
        $this->cacheDir = $cacheDir;
        // 确保目录存在
        if (!File::exists($cacheDir)) {
            File::write($cacheDir . '/.gitkeep', '');
        }
    }

    /**
     * 设置缓存目录
     * @param string $dir 缓存目录名称
     */
    public function setCacheDir($dir)
    {
        $this->cacheDir = $dir;
        // 确保目录存在
        if (!File::exists($dir)) {
            File::write($dir . '/.gitkeep', '');
        }
    }

    /**
     * 设置缓存
     * 
     * @param string $key 缓存键
     * @param mixed $value 缓存值
     * @param int $ttl 缓存时间(秒)，0表示永不过期
     * @return void
     */
    public function set($key, $value, $ttl = 0)
    {
        $cacheFile = $this->getCacheFilePath($key);
        $data = [
            'data' => $value,
            'expire' => $ttl > 0 ? time() + $ttl : 0
        ];
        File::write($cacheFile, serialize($data));
    }

    /**
     * 获取缓存
     * 
     * @param string $key 缓存键
     * @return mixed|null 缓存值或null
     */
    public function get($key)
    {
        $cacheFile = $this->getCacheFilePath($key);
        
        if (!File::exists($cacheFile)) {
            return null;
        }

        $data = unserialize(File::read($cacheFile));
        
        if ($data['expire'] > 0 && $data['expire'] < time()) {
            $this->delete($key);
            return null;
        }

        return $data['data'];
    }

    /**
     * 删除缓存
     * 
     * @param string $key 缓存键
     * @return void
     */
    public function delete($key)
    {
        $cacheFile = $this->getCacheFilePath($key);
        if (File::exists($cacheFile)) {
            File::delete($cacheFile);
        }
    }

    /**
     * 检查缓存是否存在
     * 
     * @param string $key 缓存键
     * @return bool
     */
    public function has($key)
    {
        $cacheFile = $this->getCacheFilePath($key);
        
        if (!File::exists($cacheFile)) {
            return false;
        }

        $data = unserialize(File::read($cacheFile));
        if ($data['expire'] > 0 && $data['expire'] < time()) {
            $this->delete($key);
            return false;
        }

        return true;
    }

    /**
     * 获取缓存文件路径
     * 
     * @param string $key 缓存键
     * @return string
     */
    private function getCacheFilePath($key)
    {
        return $this->cacheDir . '/' . md5($key) . '.cache';
    }

    /**
     * 生成随机 key
     *
     * @param string $prefix 可选前缀
     * @return string
     */
    public function generateRandomKey($prefix = '')
    {
        // 尝试使用更安全的 random_bytes
        try {
            $random = bin2hex(random_bytes(16));
        } catch (\Exception $e) {
            // 回退到 uniqid
            $random = uniqid('', true);
            $random = str_replace('.', '', $random);
        }

        return $prefix . $random;
    }

    
}
?>